using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MusicLibrary.Server.Models;
using MusicLibrary.Server.Data;
using System.Linq;

[Authorize]
[ApiController]
[Route("api/admin")]
public class AdminController : ControllerBase
{
    private readonly MusicLibraryContext _context;
    public AdminController(MusicLibraryContext context) => _context = context;

    // ��ҳ��ȡ�û�
    [HttpGet("users")]
    public IActionResult GetUsers(int page = 1, string search = "")
    {
        var users = _context.Users
            .Where(u => string.IsNullOrEmpty(search) || u.UserName.Contains(search))
            .Skip((page - 1) * 10).Take(10).ToList();
        var totalPages = 1; // ʵ��Ӧ������ҳ��
        return Ok(new { users, totalPages });
    }

    // ɾ���û�
    [HttpDelete("user/{id}")]
    public IActionResult DeleteUser(int id)
    {
        var user = _context.Users.Find(id);
        if (user == null) return NotFound();
        _context.Users.Remove(user);
        _context.SaveChanges();
        return Ok(new { success = true });
    }

    // ��ҳ��ȡ����
    [HttpGet("songs")]
    public IActionResult GetSongs(int page = 1, string search = "")
    {
        var songs = _context.Songs
            .Where(s => string.IsNullOrEmpty(search) || s.Title.Contains(search))
            .Skip((page - 1) * 10).Take(10).ToList();
        var totalPages = 1;
        return Ok(new { songs, totalPages });
    }

    // ɾ������
    [HttpDelete("song/{id}")]
    public IActionResult DeleteSong(int id)
    {
        var song = _context.Songs.Find(id);
        if (song == null) return NotFound();
        _context.Songs.Remove(song);
        _context.SaveChanges();
        return Ok(new { success = true });
    }
}